package com.fc.redap.controller.api;

import com.fc.redap.controller.BaseController;
import com.fc.redap.dtos.ResponseJson;
import com.fc.redap.entity.RegionEntity;
import com.fc.redap.service.IRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/region")
@Api(value="城市区域相关的API", tags={"城市区域"})
public class RegionController extends BaseController {

    @Autowired
    private IRegionService regionService;

    @ApiOperation(value = "GET -- 获取地区列表",
            notes = " REST 方式请求。"+
                    "Request Parameter: <ul>" +
                    "<li><i>可选参数: parentCode: , 例如：'0' 代表省和直辖市, '11' 代表北京市下属辖区。客户端根据省-市-区逐级查询</i></li>" +
                    "<li><i>可选参数: count: 每页记录数</i></li>" +
                    "<li><i>可选参数: page: 第几页</i></li>" +
                    "</ul>",
            response = ResponseJson.class,
            httpMethod = "GET",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    @GetMapping("/list")
    @SuppressWarnings("unchecked")
    public ResponseJson doQueryList(@RequestParam(name="parentCode", required=false) String parentCode,
                                    @RequestParam(name="count", required=false) Integer count,
                                    @RequestParam(name="page", required=false) Integer page) {
        Map<String, Object> paramMap = new HashMap();
        if(parentCode ==null || "".equals(parentCode)){
            paramMap.put("parentCode","0");
        }else{
            paramMap.put("parentCode",parentCode);
        }
        paramMap.put("limit",count);
        paramMap.put("offset",page);
        //校对并赋值 offset和limit
        calculatePagination(paramMap);

        List<RegionEntity> regionList = regionService.queryList(paramMap);
        return ResponseJson.success(regionList);
    }

}
